#include "settingsdialog.h"
#include "settingsmanager.h"
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QLabel>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QCheckBox>
#include <QtCore/QDebug>

SettingsDialog::SettingsDialog(SettingsManager *settingsManager, QWidget *parent)
    : QDialog(parent)
    , m_settingsManager(settingsManager)
{
    setupUI();
    loadSettings();
    
    connect(m_okButton, &QPushButton::clicked, this, &SettingsDialog::saveSettings);
    connect(m_cancelButton, &QPushButton::clicked, this, &QDialog::reject);
    connect(m_browseButton, &QPushButton::clicked, this, &SettingsDialog::browseDownloadPath);
    connect(m_useProxyCheckBox, &QCheckBox::toggled, m_proxyHostEdit, &QLineEdit::setEnabled);
    connect(m_useProxyCheckBox, &QCheckBox::toggled, m_proxyPortSpinBox, &QSpinBox::setEnabled);
    
    setWindowTitle("Settings");
    setMinimumWidth(400);
}

SettingsDialog::~SettingsDialog()
{
}

void SettingsDialog::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    // 下载设置组
    QGroupBox *downloadGroup = new QGroupBox("Download Settings", this);
    QFormLayout *downloadLayout = new QFormLayout(downloadGroup);
    
    m_maxConcurrentSpinBox = new QSpinBox(this);
    m_maxConcurrentSpinBox->setRange(1, 50);
    m_maxConcurrentSpinBox->setSuffix(" downloads");
    downloadLayout->addRow("Maximum concurrent downloads:", m_maxConcurrentSpinBox);
    
    m_speedLimitSpinBox = new QSpinBox(this);
    m_speedLimitSpinBox->setRange(0, 100000);
    m_speedLimitSpinBox->setSpecialValueText("No limit");
    m_speedLimitSpinBox->setSuffix(" KB/s");
    downloadLayout->addRow("Speed limit:", m_speedLimitSpinBox);
    
    QWidget *pathWidget = new QWidget(this);
    QHBoxLayout *pathLayout = new QHBoxLayout(pathWidget);
    pathLayout->setContentsMargins(0, 0, 0, 0);
    
    m_downloadPathEdit = new QLineEdit(this);
    m_browseButton = new QPushButton("Browse...", this);
    pathLayout->addWidget(m_downloadPathEdit);
    pathLayout->addWidget(m_browseButton);
    
    downloadLayout->addRow("Default download path:", pathWidget);
    
    m_useProxyCheckBox = new QCheckBox("Use proxy server", this);
    downloadLayout->addRow(m_useProxyCheckBox);
    
    QWidget *proxyWidget = new QWidget(this);
    QHBoxLayout *proxyLayout = new QHBoxLayout(proxyWidget);
    proxyLayout->setContentsMargins(0, 0, 0, 0);
    
    m_proxyHostEdit = new QLineEdit(this);
    m_proxyHostEdit->setPlaceholderText("Proxy host");
    m_proxyPortSpinBox = new QSpinBox(this);
    m_proxyPortSpinBox->setRange(1, 65535);
    m_proxyPortSpinBox->setValue(8080);
    m_proxyPortSpinBox->setFixedWidth(80);
    
    proxyLayout->addWidget(m_proxyHostEdit);
    proxyLayout->addWidget(new QLabel("Port:", this));
    proxyLayout->addWidget(m_proxyPortSpinBox);
    
    downloadLayout->addRow("Proxy settings:", proxyWidget);

    // 重试设置
    m_maxRetriesSpinBox = new QSpinBox(this);
    m_maxRetriesSpinBox->setRange(0, 10);
    m_maxRetriesSpinBox->setSuffix(" 次");
    downloadLayout->addRow("Maximum retries:", m_maxRetriesSpinBox);
    
    // 用户界面设置组
    QGroupBox *uiGroup = new QGroupBox("User Interface", this);
    QFormLayout *uiLayout = new QFormLayout(uiGroup);
    
    m_notificationCheckBox = new QCheckBox("Show notification when download completes", this);
    uiLayout->addRow(m_notificationCheckBox);
    
    m_startOnAddCheckBox = new QCheckBox("Start download immediately after adding", this);
    uiLayout->addRow(m_startOnAddCheckBox);
    
    // 添加到主布局
    mainLayout->addWidget(downloadGroup);
    mainLayout->addWidget(uiGroup);
    
    // 按钮
    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
    m_okButton = buttonBox->addButton(QDialogButtonBox::Ok);
    m_cancelButton = buttonBox->addButton(QDialogButtonBox::Cancel);
    
    mainLayout->addWidget(buttonBox);
}

void SettingsDialog::loadSettings()
{
    // 下载设置
    m_maxConcurrentSpinBox->setValue(m_settingsManager->maxConcurrentDownloads());
    m_speedLimitSpinBox->setValue(m_settingsManager->downloadSpeedLimit());
    m_downloadPathEdit->setText(m_settingsManager->defaultDownloadPath());
    m_maxRetriesSpinBox->setValue(m_settingsManager->maxRetries());
    m_useProxyCheckBox->setChecked(m_settingsManager->useProxy());
    m_proxyHostEdit->setText(m_settingsManager->proxyHost());
    m_proxyPortSpinBox->setValue(m_settingsManager->proxyPort());
    
    // 用户界面设置
    m_notificationCheckBox->setChecked(m_settingsManager->showNotificationOnComplete());
    m_startOnAddCheckBox->setChecked(m_settingsManager->startDownloadOnAdd());
    
    // 根据是否使用代理设置代理控件的启用状态
    m_proxyHostEdit->setEnabled(m_useProxyCheckBox->isChecked());
    m_proxyPortSpinBox->setEnabled(m_useProxyCheckBox->isChecked());
}

void SettingsDialog::saveSettings()
{
    // 下载设置
    m_settingsManager->setMaxConcurrentDownloads(m_maxConcurrentSpinBox->value());
    m_settingsManager->setDownloadSpeedLimit(m_speedLimitSpinBox->value());
    m_settingsManager->setDefaultDownloadPath(m_downloadPathEdit->text());
    m_settingsManager->setMaxRetries(m_maxRetriesSpinBox->value());
    m_settingsManager->setUseProxy(m_useProxyCheckBox->isChecked());
    m_settingsManager->setProxyHost(m_proxyHostEdit->text());
    m_settingsManager->setProxyPort(m_proxyPortSpinBox->value());
    
    // 用户界面设置
    m_settingsManager->setShowNotificationOnComplete(m_notificationCheckBox->isChecked());
    m_settingsManager->setStartDownloadOnAdd(m_startOnAddCheckBox->isChecked());
    
    // 保存设置
    m_settingsManager->saveSettings();
    
    accept();
}

void SettingsDialog::browseDownloadPath()
{
    QString path = QFileDialog::getExistingDirectory(this, "Select Download Directory", 
                                                     m_downloadPathEdit->text());
    if (!path.isEmpty()) {
        m_downloadPathEdit->setText(path);
    }
}